package com.cqust.service;

import com.cqust.dao.CardDao;
import com.cqust.dao.RecordDao;
import com.cqust.dao.StudentDao;
import com.cqust.entity.Card;
import com.cqust.entity.Record;
import com.cqust.entity.Student;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

@Service
public class CardService {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private CardDao cardDao;
    @Autowired
    private RecordDao recordDao;
    //学生注册
    public int studentEnroll(String sno,String name,int age,String major)
    {
        Student student=new Student();
        student.setSno(sno);
        student.setName(name);
        student.setAge(age);
        student.setMajor(major);
        studentDao.insert(student);
        return 1;
    }
    //卡开户
    public int openAccount(String sno,String name,String cno,float balance)
    {
//        Scanner sc = new Scanner(System.in);
//        System.out.print("请输入学生学号：");
//        String sno=sc.nextLine();
        String strWhere=" sno='%s'";
        strWhere= String.format(strWhere, sno);
        List<Student> list=studentDao.selectByCondition(strWhere);
        if(list.size()>0)
        {
            Card card=new Card();
            card.setSid(list.get(0).getSid());
            card.setSno(list.get(0).getSno());
//            System.out.print("请输入余额：");
//            int balance=sc.nextInt();
            card.setBalance(balance);
//            System.out.println("请输入卡号：");
//            String cno=sc.nextLine();
            card.setCno(cno);
//            System.out.println("请输入姓名：");
//            String name=sc.nextLine();
            card.setName(name);
            return cardDao.insert(card);
        }
        else
            return -1;
    }

    //消费以及查看余额
    public int consume(String sno,float money)
    {
        String strWhere=" sno='%s'";
        strWhere= String.format(strWhere, sno);
        List<Card> cards=cardDao.selectByCondition(strWhere);

        if(cards.size()>0)
        {
            Card card=new Card();
            Record record=new Record();
            card.setSno(cards.get(0).getSno());
            card.setBalance(cards.get(0).getBalance()-money);
            record.setCid(cards.get(0).getCid());

            //获取当前时间
            Date date=new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentTime=df.format(date);
            record.setConsumeTime(currentTime);
            record.setConsumeMoney(money);
            //将消费记录更新到record表
            recordDao.insert(record);
            return cardDao.updateById(card);
        }
        else
            return -1;
    }

    //查询消费记录queryConsumeRecord
    public int queryConsumeRecord(String strWhere)
    {
        if(strWhere.equals(""))
        {
            List<Record> records=recordDao.selectByCondition(strWhere);
            if(records.size()>0)
            {
                System.out.println("消费记录为："+records);
                return 1;
            }
        }
        else
        {
            String strWhere1=" rid=%s";
            strWhere= String.format(strWhere1, strWhere);
            List<Record> records=recordDao.selectByCondition(strWhere);
            if(records.size()>0)
            {
                System.out.println("消费记录为："+records);
                return 1;
            }
            else
            {
                System.out.println("没有该条记录！！");
                return -1;
            }
        }
        return 0;
    }
}
